package com.example.controller;

import com.example.common.annotation.OperLog;
import com.example.common.annotation.RepeatSubmit;
import com.example.common.enums.CodeEnum;
import com.example.common.enums.LogType;
import com.example.common.utils.R;
import com.example.system.dto.UserDTO;
import com.example.system.entity.User;
import com.example.system.service.IUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 王鹤蒲
 * @since 2023-07-31
 */
@RestController
@Api(tags = "人员信息API",description="提供人员信息相关的 REST API")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService iUserService;

    @RequestMapping(value="/selectAll" ,method= RequestMethod.GET)
    @ResponseBody
    public R selectAll(User user, Integer currentPage, Integer pageSize)  {
        System.out.println(user+" "+currentPage+" "+pageSize);
       return R.success(iUserService.selectAll(user,currentPage,pageSize)) ;
    }

    @OperLog(operation = "用戶管理",type = LogType.DELETE)
    @RequestMapping(value="/delete/{id}",method=RequestMethod.DELETE)
    public R deleteById(@PathVariable("id") Integer id)  {
        if(iUserService.deleteById(id)){
            return R.success();
        }else{
            return R.error(CodeEnum.DELETE_ERROR.getCode(), CodeEnum.DELETE_ERROR.getMsg());
        }
    }

    @OperLog(operation = "用戶管理",type = LogType.BATCH_DELETE)
    @RequestMapping(value="/batchDelete",method=RequestMethod.POST)
    public R deleteByIds(@RequestBody Integer ids[]){
        if(iUserService.deleteByIds(ids)){
            return R.success();
        }else{
            return R.error(CodeEnum.DELETE_ERROR.getCode(), CodeEnum.DELETE_ERROR.getMsg());
        }
    }

    @OperLog(operation = "用戶管理",type = LogType.LOGIN)
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public R login( @RequestBody UserDTO userDTO) throws ClassNotFoundException {
        return R.success(iUserService.login(userDTO));
    }

    @OperLog(operation = "用戶管理",type = LogType.REGISTER)
    @RepeatSubmit(lockTime = 5)
    @RequestMapping(value="/register",method=RequestMethod.POST)
    @ResponseBody
    public R register(@RequestBody UserDTO userDTO){
        System.out.println(userDTO);
        return iUserService.register(userDTO);
    }


    @OperLog(operation = "用戶管理",type = LogType.UPDATE)
    @RepeatSubmit(lockTime = 5)
    @RequestMapping(value="/updateUser",method=RequestMethod.POST)
    public R updateUser(@RequestBody UserDTO userDTO){
     return  R.success(iUserService.updateUser(userDTO));
    }


    @RequestMapping(value="/userName/{userName}",method=RequestMethod.GET)
    public R getUserByUserName(@PathVariable("userName") String userName){
        return R.success(iUserService.getUserByUserName(userName));
    }

    @RequestMapping(value="/export",method= RequestMethod.GET)
    @RepeatSubmit(lockTime = 5)
    public void export(HttpServletResponse response) {
            iUserService.export1(response);
    }

    @OperLog(operation = "用戶管理",type = LogType.ADD)
    @RepeatSubmit(lockTime = 5)
    @RequestMapping(value="/save",method=RequestMethod.POST)
    public R save(@RequestBody @Valid UserDTO userDTO){
        return iUserService.save(userDTO);
    }

    @OperLog(operation = "用戶管理",type = LogType.UPDATE)
    @RepeatSubmit(lockTime = 5)
    @RequestMapping(value="/edit/{userId}",method=RequestMethod.POST)
    public R editUser(@PathVariable Integer userId,@RequestBody UserDTO userDTO){
        return iUserService.edit(userId,userDTO);
    }

    @RequestMapping(value="/selectUserByUserId/{userId}",method=RequestMethod.GET)
    public R selectUserByUserId(@PathVariable("userId") Integer userId){
        return R.success(iUserService.getById(userId));
    }


    @RequestMapping(value="/getCaptcha",method = RequestMethod.GET)
    @ResponseBody
    public R generateCaptchaImage() {
       return iUserService.generateCaptchaImage();
    }

    @RequestMapping(value="/sendEmailCode",method=RequestMethod.GET)
    @ResponseBody
    public R sendEmailCode(String email){
       return iUserService.sendEmailCode(email);
    }

    @RequestMapping(value="/verifyEmailCode",method=RequestMethod.GET)
    @ResponseBody
    public R verifyEmailCode(String email, String dynamicCode){
        System.out.println("------------");
        return iUserService.verifyEmailCode(email,dynamicCode);
    }

    @RequestMapping(value="/isPhonenumberExist",method=RequestMethod.GET)
    @ResponseBody
    public R isPhonenumberExist(String phonenumber){
        return iUserService.isPhonenumberExist(phonenumber);
    }

    @RequestMapping(value="/isUsernameExist",method=RequestMethod.GET)
    @ResponseBody
    public R isUsernameExist(String userName){
        return iUserService.isUsernameExist(userName);
    }

    @RequestMapping(value="/isEmailExist",method=RequestMethod.GET)
    @ResponseBody
    public R isEamilExist(String email){
        return iUserService.isEmailExist(email);
    }



    @RequestMapping(value="/verifyUserEmail",method=RequestMethod.GET)
    @ResponseBody
    public R verifyUserEmail(String userName,String email) {
        return iUserService.verifyUserEmail(userName,email);
    }

    @RequestMapping(value="/verifyOriginalPassword",method=RequestMethod.GET)
    @ResponseBody
    public R verifyOriginalPassword(String userName,String originalPassword){
        return iUserService.verifyPassword(userName,originalPassword);
    }

    @RequestMapping(value="/updatePassword",method=RequestMethod.GET)
    @ResponseBody
    public R updatePasswordByUserName(String userName, String password){
        return iUserService.updatePasswordByUserName(userName,password);
    }


    @RequestMapping(value="/updateAvatar",method=RequestMethod.GET)
    @RepeatSubmit(lockTime = 5)
    @ResponseBody
    public R updateAvatarByUserName(@RequestParam String userName,@RequestParam String avatar){
         return iUserService.updateAvatarByUserName(userName,avatar);
    }

    @RequestMapping(value="/logout",method=RequestMethod.GET)
    @ResponseBody
    public R logout(){
        return iUserService.logout();
    }


    @RequestMapping(value="/import",method=RequestMethod.POST)
    @RepeatSubmit(lockTime = 5)
    @ResponseBody
    public R  importExcel(@RequestParam("file") MultipartFile file) throws IOException {
            return  iUserService.importExcel(file);
    }

    @RequestMapping(value="/generateImportTemplate",method= RequestMethod.GET)
    @RepeatSubmit(lockTime = 5)
    public void generateImportTemplate(HttpServletResponse response) {
        iUserService.generateUserInfomationImportTemplate(response);
    }
}


